import axios from 'axios'
const baseURL = 'http://localhost:8888/api/private/v1/'
// const baseURL = 'http://47.96.21.88:8082/api/private/v1/'
axios.defaults.baseURL = baseURL

// 添加拦截器
axios.interceptors.request.use(function (config) {
  // 将token给到一个前后台约定好的key中，作为请求发送
  let token = localStorage.getItem('wtoken')
  if (token) {
    config.headers['Authorization'] = token
  }
  return config
}, function (error) {
  return Promise.reject(error)
})

export const login = (params) => {
  // console.log(12312312312, params)
  return axios.post('login', params)
    .then((result) => {
      return result.data
    })
}

export const getUserList = (params) => {
  return axios.get('users', params)
    .then((result) => {
      // console.log(result.data)
      return result.data
    })
}

export const addUsers = (params) => {
  return axios.post('users', params)
    .then((result) => {
      // console.log(result.data)
      return result.data
    })
}

export const editUsers = (params) => {
  return axios.put(`users/${params.id}`, params)
    .then((result) => {
      return result.data
    })
}

export const deleteUsers = (id) => {
  return axios.delete(`users/${id}`)
    .then((result) => {
      return result.data
    })
}

// 获取授权角色的信息
export const getRolesList = () => {
  return axios.get('roles')
    .then((result) => {
      return result.data
    })
}

export const grantUser = (params) => {
  return axios.put(`users/${params.id}/role`, {rid: params.rid})
    .then((result) => {
      return result.data
    })
}

// 切换用户状态
export const changeStatus = (params) => {
  return axios.put(`users/${params.uid}/state/${params.type}`)
    .then((result) => {
      return result.data
    })
}

// 获取权限列表
export const getRightsList = (params) => {
  return axios.get(`rights/${params.type}`)
    .then((result) => {
      return result.data
    })
}

// 权限修改
export const rightsChange = (roleId, rids) => {
  return axios.post(`roles/${roleId}/rights`, rids)
    .then((result) => {
      return result.data
    })
}

// 删除权限
export const deleterolesrights = (roleId, rightId) => {
  return axios.delete(`roles/${roleId}/rights/${rightId}`)
    .then((result) => {
      return result.data
    })
}

// 添加角色
export const addrolesderights = (params) => {
  return axios.post(`roles`, params)
    .then((result) => {
      return result.data
    })
}
// 编辑提交角色
export const editAddRoles = (params) => {
  return axios.put(`roles/${params.id}`, params)
    .then((result) => {
      return result.data
    })
}

// 删除角色
export const delroles = (id) => {
  return axios.delete(`roles/${id}`)
    .then((result) => {
      return result.data
    })
}

// 获取左侧菜单权限
export const getMenu = () => {
  return axios.get(`menus`)
    .then((result) => {
      return result.data
    })
}

// 获取商品分类列表
export const getcategerieslist = (pa) => {
  return axios.get(`categories`, {params: pa})
    .then((result) => {
      return result.data
    })
}

// 添加商品分类
export const addCategory = (pa) => {
  return axios.post(`categories`, pa)
    .then((result) => {
      return result.data
    })
}
